package com.yumis.service.modules.user.controller;

import com.yumis.service.common.enums.HttpState;
import com.yumis.service.common.vo.Response;
import com.yumis.service.model.pojo.Staff;
import com.yumis.service.modules.user.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("staff")
public class StaffController {

    @Autowired
    private StaffService service;

    /**
     * 注册
     * @param staff 实体类
     * @return
     */
    @PostMapping("register")
    public ResponseEntity<Response> register(@Valid Staff staff) {
        service.register(staff);
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK));
    }

    /**
     * 登录
     * @param phone
     * @param password
     * @return
     */
    @PostMapping("login")
    public ResponseEntity<Response> login(@RequestParam("phone") String phone, @RequestParam("password") String password){
        return ResponseEntity.ok(Response.buildResponse(HttpState.OK, service.login(phone, password)));
    }

    /**
     * 员工分页
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<Response> queryStaffs(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer rows
    ){
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK,service.queryStaffs(page,rows)));
    }


    @PostMapping("edit")
    public ResponseEntity<Response> editStaffs(@Valid Staff staff){
        service.editStaffs(staff);
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK));
    }


    @GetMapping("search")
    public ResponseEntity<Response> queryStaffsByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "10") Integer rows,
            @RequestParam(value = "key", required = false) String key

    ){
        return ResponseEntity.ok().body(Response.buildResponse(HttpState.OK,service.queryStaffsByPage(page,rows,key)));
    }

}
